package net.coding.api.test.common;

import com.jcraft.jsch.Identity;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.KeyPair;

/**
 * Created with IntelliJ IDEA.
 * User: chenfeilong
 * Date: 2018/1/26
 * Time: 14:12
 * Description:
 */
public class SshKeyIdentity implements Identity {
    private KeyPair keyPair;

    SshKeyIdentity(KeyPair keyPair) throws JSchException {
        this.keyPair = keyPair;
    }

    @Override
    public boolean setPassphrase(byte[] passphrase) throws JSchException {
        return this.keyPair.decrypt(passphrase);
    }

    @Override
    public byte[] getPublicKeyBlob() {
        return this.keyPair.getPublicKeyBlob();
    }

    @Override
    public byte[] getSignature(byte[] data) {
        return this.keyPair.getSignature(data);
    }

    /**
     * @deprecated
     */
    @Override
    public boolean decrypt() {
        throw new RuntimeException("not implemented");
    }

    @Override
    public String getAlgName() {
        return "ssh-rsa";
    }

    @Override
    public String getName() {
        return "test";
    }

    @Override
    public boolean isEncrypted() {
        return this.keyPair.isEncrypted();
    }

    @Override
    public void clear() {
        this.keyPair.dispose();
        this.keyPair = null;
    }

    public KeyPair getKeyPair() {
        return this.keyPair;
    }
}
